package com.cropdoctor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cropdoctor.common.CustomException;
import com.cropdoctor.common.Result;
import com.cropdoctor.mapper.FeedBackMapper;
import com.cropdoctor.model.dto.FeedBackDto;
import com.cropdoctor.model.po.FeedBackPo;
import com.cropdoctor.model.vo.FeedBackVO;
import com.cropdoctor.service.FeedBackService;
import com.cropdoctor.util.ContextGet;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class FeedBackServiceImpl implements FeedBackService {
    @Autowired
    FeedBackMapper feedBackMapper;
    @Override
    public Result<String> submitFeedBack(FeedBackPo feedBackPo) {
        long userId = ContextGet.getUserId();
        feedBackPo.setUserId(userId);
        int result = feedBackMapper.insert(feedBackPo);

        //若提交失败，抛出CustomException异常
        if (result <= 0){
            throw new CustomException("用户反馈数据提交失败");
        }
        else return Result.success("提交成功");
    }

    @Override
    public Result<List<FeedBackDto>> getHistoryFeedBack(long idUser) {
        //构造查询条件
        LambdaQueryWrapper<FeedBackPo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FeedBackPo::getUserId, idUser);

        //查询数据
        List<FeedBackPo> feedBackPos = feedBackMapper.selectList(wrapper);

        //若用户的历史数据为空,抛出CustomException异常
        if (feedBackPos.size() == 0) throw new CustomException("该用户的历史数据为空");

        //封装数据
        ArrayList<FeedBackDto> list = new ArrayList<>();
        feedBackPos.forEach(item ->{
            FeedBackDto feedBackDto = new FeedBackDto();
            BeanUtils.copyProperties(item,feedBackDto);
            list.add(feedBackDto);
        });

        //返回数据
        return Result.success(list);
    }

    @Override
    public Result<List<FeedBackDto>> getAllFeedBack() {
        //构造查询条件
        LambdaQueryWrapper<FeedBackPo> wrapper = new LambdaQueryWrapper<>();

        //查询数据
        List<FeedBackPo> feedBackPos = feedBackMapper.selectList(wrapper);

        //若用户的历史数据为空,抛出CustomException异常
        if (feedBackPos.size() == 0) throw new CustomException("该用户的历史数据为空");

        //封装数据
        ArrayList<FeedBackDto> list = new ArrayList<>();
        feedBackPos.forEach(item ->{
            FeedBackDto feedBackDto = new FeedBackDto();
            BeanUtils.copyProperties(item,feedBackDto);
            list.add(feedBackDto);
        });

        //返回数据
        return Result.success(list);
    }

    @Override
    public Result<String> adminDeleteById(Long id) {
        int result = feedBackMapper.deleteById(id);

        //若删除失败，抛出CustomException异常
        if (result <= 0){
            throw new CustomException("用户反馈数据删除失败");
        }
        else return Result.success("删除成功");
    }

    @Override
    public Result<String> updateFeedBack(FeedBackPo feedBackPo) {
        int result = feedBackMapper.updateById(feedBackPo);

        //若更新失败，抛出CustomException异常
        if (result <= 0){
            throw new CustomException("用户反馈数据更新失败");
        }
        else return Result.success("更新成功");
    }

    @Override
    @Transactional
    public Result<String> userDeleteById(Long id) {
        FeedBackPo feedBackPo = feedBackMapper.selectById(id);
        Long dbUserId = feedBackPo.getUserId();
        Long seuUserId = ContextGet.getUserId();
        if (!dbUserId.equals(seuUserId)) throw new CustomException("只能删除自己的反馈记录！");
        int result = feedBackMapper.deleteById(id);

        //若删除失败，抛出CustomException异常
        if (result <= 0){
            throw new CustomException("用户反馈数据删除失败");
        }
        else return Result.success("删除成功");
    }

    @Override
    public Result<List<FeedBackVO>> adminGetAllFeedBack() {
        return Result.success(feedBackMapper.selectListWithUser());
    }
}
